package com.net.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.net.annotaion.ApiLog;
import com.net.controller.model.ResponseModel;
import com.net.controller.vo.EditWebsiteParam;
import com.net.controller.vo.ListPageParam;
import com.net.controller.vo.WebsiteResponseModel;
import com.net.controller.vo.WebsiteTypeResponseModel;
import com.net.model.Pack;
import com.net.model.Page;
import com.net.model.PageData;
import com.net.model.User;
import com.net.model.Website;
import com.net.model.WebsiteType;
import com.net.service.vo.ServiceCode;
import com.net.service.website.WebsiteService;
import com.net.util.ResponseEntityHelper;

@Controller
@RequestMapping(value = "/net/detect")
public class WebsiteTypeApi extends SuperController {
	WebsiteService websiteService;

	@Resource(name = "websiteService")
	public void setWebsiteService(WebsiteService websiteService) {
		this.websiteService = websiteService;
	}

	@ApiLog
	@RequestMapping(value = "/websitetype/tolistpage")
	public ModelAndView toListPage(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("website/type_manage");
		return mv;
	}
	
	@ApiLog
	@RequestMapping(value = "/websitetype/listall", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> listPageWebsiteType(HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<WebsiteType> typeList = websiteService.getAllType();
		WebsiteTypeResponseModel wtrm = new WebsiteTypeResponseModel();
		wtrm.setTypeList(typeList);
		return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "获取成功",wtrm);
	}
	
	
	@ApiLog
	@RequestMapping(value = "/websitetype/del", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> delWebsiteType(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		boolean res = websiteService.delWebsiteType(param.getId());
		if(res == true){
			return ResponseEntityHelper.createResponse(ServiceCode.SUCCESS, "删除成功");
		}
		return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "服务器易异常");
	}
	
	
	@ApiLog
	@RequestMapping(value = "/websitetype/edit", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> editWebsiteType(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		int id = param.getId();
		String name = param.getDomain();
		String note = param.getType();
		if(id == 0 || "".equals(id)){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "参数错误");
		}
		
		
		ResponseModel res = websiteService.editWebsiteType(id, name, note);
		return ResponseEntityHelper.returnResponse(res);
	}
	
	
	@ApiLog
	@RequestMapping(value = "/websitetype/add", method = RequestMethod.POST)
	public ResponseEntity<ResponseModel> addWebsiteType(@RequestBody EditWebsiteParam param, HttpServletRequest request) {
		session = request.getSession();
		User user = (User) session.getAttribute("user");
		String name = param.getDomain();
		String note = param.getType();
		if(name == null || "".equals(name)){
			return ResponseEntityHelper.createResponse(ServiceCode.FAIL, "请输入类型名称");
		}
		ResponseModel res = websiteService.addWebsiteType(name, note);
		return ResponseEntityHelper.returnResponse(res);
	}
}
